<?php
namespace App\Service\Imessage;

use App\Common\Fun;

class wxMessage
{
    public static function checkSignature($signature,$timestamp,$nonce)
    {
        $wx_config = config('weixin.wx_message');;
        $token = $wx_config['token'];
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if ($tmpStr == $signature ) {
            return true;
        } else {
            return false;
        }
    }

    public static function getToken()
    {
        $program_config = config('weixin.wx_program');
        $data = array(
            'grant_type' => 'client_credential',
            'appid' => $program_config['appid'],
            'secret' => $program_config['secret']
        );
        $res = Fun::sendUrl('get','https://api.weixin.qq.com/cgi-bin/token',$data);
        return json_decode($res,true);
    }
}